package com.woniu.util;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeanUtils;

public class WoniuBeanUtils {

	public static <T> T copyProperties(Object obj,Class<T> type) {
		T o = null;
		try {
			o = type.getConstructor().newInstance();
			BeanUtils.copyProperties(obj, o);
		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
				| NoSuchMethodException | SecurityException e) {
			e.printStackTrace();
		}
		return o;
	}
	
	public static <T> List<T> copyList(Collection<?> objs,Class<T> type) {
		List<T> lists = new ArrayList<>();
		try {
			for(Object obj : objs) {
				T o = type.getConstructor().newInstance();
				BeanUtils.copyProperties(obj, o);
				lists.add(o);
			}
		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
				| NoSuchMethodException | SecurityException e) {
			e.printStackTrace();
		}
		return lists;
	}
	
	public static <T> Set<T> copySet(Collection<?> objs,Class<T> type) {
		Set<T> sets = new HashSet<>();
		try {
			for(Object obj : objs) {
				T o = type.getConstructor().newInstance();
				BeanUtils.copyProperties(obj, o);
				sets.add(o);
			}
		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
				| NoSuchMethodException | SecurityException e) {
			e.printStackTrace();
		}
		return sets;
	}
}
